package blog.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import blog.entity.Link;
import blog.service.LinkService;

@RequestMapping("link")
@Controller
public class LinkController {
	
	@Autowired
	private LinkService linkService;
	@RequestMapping("")
	public String link(ModelMap map) {
		List<Link> findAllLink = linkService.findAllLink();
		map.put("linkList", findAllLink);
		return "link/link_list";
	}
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public String goLinkAdd(ModelMap map) {
		List<Link> findAllLink = linkService.findAllLink();
		map.put("linkList", findAllLink);
		return "link/link_add";
	}
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public String linkAdd(Link link) {
		if(link.getLinkOrder()==null) {
			link.setLinkOrder(0);
		}
		linkService.addLink(link);
		return "forward:/link";
	}
	
	/**
	 * 去修改界面
	 * @param pageId
	 * @return
	 */
	@RequestMapping(value="/edit/{linkId}",method=RequestMethod.GET)
	public String linkGoEdit(ModelMap map,@PathVariable("linkId") Integer linkId) {
		Link  link =linkService.getLinkById(linkId);
		map.put("link", link);
		map.put("flag", "edit");
		return "forward:/link/add";
	}
	/**
	 * 修改页面
	 * @param page
	 * @return
	 */
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	public String linkEdit(ModelMap map,Link link) {
		List<Link> findAllLink = linkService.findAllLink();
		map.put("linkList", findAllLink);
		linkService.editLink(link);
		return "forward:/link";
	}
	@RequestMapping("/delete/{pageId}")
	public String pageDelete(@PathVariable("pageId")Integer linkId) {
		linkService.linkDelete(linkId);
		return "forward:/link";
	}
}
